
package com.feib.soeasy.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import com.feib.soeasy.model.Role;
import com.feib.soeasy.model.User;


/**
 * @title (#)SoeasyUserDetails.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/15 create by Jimmy Liu<br>
 */
public class SoeasyUserDetails extends User implements UserDetails {
    
    private static final long serialVersionUID = -5146400849191518294L;

    private User user;    

    private Collection<GrantedAuthority> auths ;
    
    public SoeasyUserDetails(User user)
    {
        Assert.notNull(user, "user can't be null");
        this.user = user;
        
        auths = new ArrayList<GrantedAuthority>();

        // 獲得用戶的角色
        Set<Role> roles = user.getRoles();
        for (Role r : roles) {
            GrantedAuthorityImpl grantedAuthorityImpl = new GrantedAuthorityImpl(r.getRolePk()+ "_" + r.getRoleName());
            if (logger.isDebugEnabled()) {
                logger.debug("用戶：[" + user.getUserName() + "]， 擁有角色：[" + r.getRolePk()+ "_" + r.getRoleName() + "]");
            }
            auths.add(grantedAuthorityImpl);
        }
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
     */
    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return auths;
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#getPassword()
     */
    @Override
    public String getPassword() {
        return user.getUserPassWord();
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
     */
    @Override
    public String getUsername() {
        return user.getUserId();
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
     */
    @Override
    public boolean isAccountNonLocked() {
        return (null == user.getLocked() ? true : !user.getLocked().booleanValue());
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return !new Date().after(user.getPwdExpiredDate());
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
     */
    @Override
    public boolean isEnabled() {
        return (null == user.getEnabled() ? true :user.getEnabled().booleanValue());
    }

    
    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

}
